package com.pg.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pg.auth.model.dto.sysrole.SysRoleConvertor;
import com.pg.auth.model.dto.sysrole.SysRoleDTO;
import com.pg.auth.model.dto.sysrole.SysRoleReq;
import com.pg.auth.model.dto.sysrole.SysRoleVO;
import com.pg.auth.model.dto.sysrolemenu.SysRoleMenuVO;
import com.pg.auth.model.entity.SysRole;
import com.pg.auth.model.mapper.SysRoleMapper;
import com.pg.auth.service.SysRoleMenuService;
import com.pg.auth.service.SysRoleService;
import com.pg.base.component.PageVO;
import com.pg.base.component.Pager;
import com.pg.base.enums.DeleteEnum;
import com.pg.base.vo.LoginVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/***
 * SysRoleServiceImpl
 * @author : huhuaping
 * @Date : 2024-06-06 20:59:34
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;

    private final SysRoleMenuService sysRoleMenuService;
    @Override
    public PageVO<SysRoleVO> page(Pager<SysRoleReq, SysRoleVO> pageReq) {

        QueryWrapper<SysRole> queryWrapper = baseQueryWrapper(pageReq.getParam());

        PageVO<SysRoleVO> result = sysRoleMapper.selectPage(pageReq.getPageVO(), queryWrapper);
        return result;
    }

    @Override
    public List<SysRoleVO> list(SysRoleReq sysRoleReq) {

        QueryWrapper<SysRole> queryWrapper = baseQueryWrapper(sysRoleReq);

        List<SysRole> sysRoleList = sysRoleMapper.selectList(queryWrapper);

        return SysRoleConvertor.INSTANCE.listDbToVO(sysRoleList);

    }

    @Override
    public SysRoleVO getById(Long roleId) {
        SysRole sysRole = sysRoleMapper.selectById(roleId);

        SysRoleVO sysRoleVO = SysRoleConvertor.INSTANCE.dbToVO(sysRole);

        List<SysRoleMenuVO> sysRoleMenuVOList = sysRoleMenuService.listByRoleId(roleId);

        if (ObjectUtil.isNotEmpty(sysRoleMenuVOList)) {
            sysRoleVO.setMenuIdList(sysRoleMenuVOList.stream().map(SysRoleMenuVO::getMenuId).collect(Collectors.toList()));
        }

        return sysRoleVO;
    }

    @Override
    public Long add(SysRoleDTO sysRoleDTO, LoginVO userVo) {

        SysRole sysRoleAdd = SysRoleConvertor.INSTANCE.dtoToDB(sysRoleDTO);
        sysRoleMapper.insert(sysRoleAdd);

        // 保存角色和菜单关系
        sysRoleMenuService.save(sysRoleAdd.getRoleId(),sysRoleDTO.getMenuIdList(),userVo);

        return sysRoleAdd.getRoleId();
    }

    @Override
    public Integer updateById(SysRoleDTO sysRoleDTO, LoginVO userVo) {
        SysRole sysRoleAdd = SysRoleConvertor.INSTANCE.dtoToDB(sysRoleDTO);
        int count = sysRoleMapper.updateById(sysRoleAdd);

        // 保存角色和菜单关系
        sysRoleMenuService.save(sysRoleAdd.getRoleId(),sysRoleDTO.getMenuIdList(),userVo);

        return count;
    }

    @Override
    public int deleteById(Long roleId) {
        SysRole sysRole = sysRoleMapper.selectById(roleId);

        // 封装对象
        SysRole sysRoleDelete = new SysRole();

        sysRoleDelete.setRoleId(roleId);
        sysRoleDelete.setDeleteStatus(DeleteEnum.DELETE.getValue());

        return sysRoleMapper.updateById(sysRoleDelete);

    }


    private QueryWrapper<SysRole> baseQueryWrapper(SysRoleReq sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysRole> lambda = queryWrapper.lambda();
        lambda.eq(SysRole::getDeleteStatus, DeleteEnum.NORMAL.getValue());
        lambda.orderByDesc(SysRole::getRoleId);

        if (sysRole == null) {
            return queryWrapper;
        }
        if (ObjectUtil.isNotEmpty(sysRole.getRoleId())) {
            lambda.eq(SysRole::getRoleId, sysRole.getRoleId());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getRoleName())) {
            lambda.eq(SysRole::getRoleName, sysRole.getRoleName());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getRoleType())) {
            lambda.eq(SysRole::getRoleType, sysRole.getRoleType());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getParentId())) {
            lambda.eq(SysRole::getParentId, sysRole.getParentId());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getRemark())) {
            lambda.eq(SysRole::getRemark, sysRole.getRemark());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getCreateOrgId())) {
            lambda.eq(SysRole::getCreateOrgId, sysRole.getCreateOrgId());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getTenantId())) {
            lambda.eq(SysRole::getTenantId, sysRole.getTenantId());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getOrgId())) {
            lambda.eq(SysRole::getOrgId, sysRole.getOrgId());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getCreateUser())) {
            lambda.eq(SysRole::getCreateUser, sysRole.getCreateUser());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getCreateUserName())) {
            lambda.eq(SysRole::getCreateUserName, sysRole.getCreateUserName());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getCreateTime())) {
            lambda.eq(SysRole::getCreateTime, sysRole.getCreateTime());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getUpdateUser())) {
            lambda.eq(SysRole::getUpdateUser, sysRole.getUpdateUser());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getUpdateUserName())) {
            lambda.eq(SysRole::getUpdateUserName, sysRole.getUpdateUserName());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getUpdateTime())) {
            lambda.eq(SysRole::getUpdateTime, sysRole.getUpdateTime());
        }
        if (ObjectUtil.isNotEmpty(sysRole.getDeleteStatus())) {
            lambda.eq(SysRole::getDeleteStatus, sysRole.getDeleteStatus());
        }
        return queryWrapper;
    }

}
